package com.tuanzi.loan.business.repository.settlementsystem.impl;

import com.tuanzi.loan.business.repository.settlementsystem.custom.SettlementPrepayRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.settlementsystem.SettlementPrepayRequest;
import com.tuanzi.loan.vo.response.settlementsystem.SettlementPrepayResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSettlementPrepay.LOAN_SETTLEMENT_PREPAY;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:24
 */
public class SettlementPrepayRepositoryImpl  extends JOOQBaseRepositoryPlus implements SettlementPrepayRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_SETTLEMENT_PREPAY;
    }

    @Override
    public Page<SettlementPrepayResponse> list(SettlementPrepayRequest searchRequest, Pageable pageable) {
        Long companyId = searchRequest.getCompanyId();
        String companyName = searchRequest.getCompanyName();
        Integer billStatus = searchRequest.getBillStatus();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        Integer receiveCompany = searchRequest.getReceiveCompany();
        Integer payMode = searchRequest.getPayMode();
        Integer payType = searchRequest.getPayType();
        String creator = searchRequest.getCreator();
        String payAccount = searchRequest.getPayAccount();
        String payName = searchRequest.getPayName();
        fields(LOAN_SETTLEMENT_PREPAY.fields());
        eq(LOAN_SETTLEMENT_PREPAY.COMPANY_ID,companyId);
        eq(LOAN_SETTLEMENT_PREPAY.BILL_STATUS,billStatus);
        eq(LOAN_SETTLEMENT_PREPAY.RECEIVE_COMPANY,receiveCompany);
        eq(LOAN_SETTLEMENT_PREPAY.PAY_TYPE,payType);
        eq(LOAN_SETTLEMENT_PREPAY.PAY_MODE,payMode);
        eq(LOAN_SETTLEMENT_PREPAY.CREATOR,creator);
        contains(LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT,payAccount);
        contains(LOAN_SETTLEMENT_PREPAY.PAY_NAME,payName);
        between(LOAN_SETTLEMENT_PREPAY.PREPAY_TIME,startTime,endTime);
        orderBy(LOAN_SETTLEMENT_PREPAY.CREATE_TIME, Sort.Direction.DESC);

        Page<SettlementPrepayResponse> search = search(pageable, record -> {
            SettlementPrepayResponse response = new SettlementPrepayResponse();
            response.setId(record.get(LOAN_SETTLEMENT_PREPAY.ID));
            response.setCompanyId(record.get(LOAN_SETTLEMENT_PREPAY.COMPANY_ID));
            response.setCompanyName(record.get(LOAN_SETTLEMENT_PREPAY.COMPANY_NAME));
            response.setPrepayTime(record.get(LOAN_SETTLEMENT_PREPAY.PREPAY_TIME));
            response.setLastPrepayAmount(record.get(LOAN_SETTLEMENT_PREPAY.LAST_PREPAY_AMOUNT));
            response.setPrepayAmount(record.get(LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT));
            response.setPayMode(record.get(LOAN_SETTLEMENT_PREPAY.PAY_MODE));
            response.setPayName(record.get(LOAN_SETTLEMENT_PREPAY.PAY_NAME));
            response.setPayAccount(record.get(LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT));
            response.setPayType(record.get(LOAN_SETTLEMENT_PREPAY.PAY_TYPE));
            response.setReceiveCompany(record.get(LOAN_SETTLEMENT_PREPAY.RECEIVE_COMPANY));
            response.setBillId(record.get(LOAN_SETTLEMENT_PREPAY.BILL_ID));
            response.setBillStatus(record.get(LOAN_SETTLEMENT_PREPAY.BILL_STATUS));
            response.setRemark(record.get(LOAN_SETTLEMENT_PREPAY.REMARK));
            response.setCreator(record.get(LOAN_SETTLEMENT_PREPAY.CREATOR));
            response.setCreateTime(record.get(LOAN_SETTLEMENT_PREPAY.CREATE_TIME));
            response.setUpdator(record.get(LOAN_SETTLEMENT_PREPAY.UPDATOR));
            response.setUpdateTime(record.get(LOAN_SETTLEMENT_PREPAY.UPDATE_TIME));
            return response;
        });
        return search;
    }
}
